
package com.kugou.game.sdk.ui.fragment;

import com.kugou.framework.base.BaseWorkerFragment;
import com.kugou.framework.v4.Fragment;
import com.kugou.framework.v4.ViewPager;
import com.kugou.game.sdk.adapter.CommonViewPagerAdapter;
import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.ui.activity.CommonSdkActivity;
import com.kugou.game.sdk.ui.dialog.CouponHelpDialog;
import com.kugou.game.sdk.ui.widget.indicator.TabPageIndicator;
import com.kugou.game.sdk.ui.widget.indicator.UnderlinePageIndicatorEx;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 描述: 我的代金券界面
 *
 * @author jamywang
 * @since 2016年5月19日 下午4:08:29
 */
public class MyCouponFragment extends BaseWorkerFragment {

    private Activity context;

    private User user;

    private ViewPager mViewPager;

    private TabPageIndicator mTabPageIndicator;

    private UnderlinePageIndicatorEx mUnderlinePageIndicatorEx;

    private TextView tvAccount, tvKubiNum;

    private RelativeLayout rlTopLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        user = UserManager.getInstance().getCurrentUserInternal();

        CommonSdkActivity activity = (CommonSdkActivity) getActivity();
        activity.setHelpImageResource(Rx.drawable.kg_coupon_activity_help_img);
        activity.setOnclickHelpImageListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                CouponHelpDialog dialog = new CouponHelpDialog(context);
                dialog.show();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = null;
        view = inflater.inflate(Rx.layout.kg_fragment_my_coupon, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        rlTopLayout = (RelativeLayout) view.findViewById(Rx.id.kg_fragment_my_coupon_top);
        tvAccount = (TextView) view.findViewById(Rx.id.kg_account_tv);
        tvAccount.setText("当前账号：" + user.getAccountName());
        tvKubiNum = (TextView) view.findViewById(Rx.id.kg_youbi_tv);
        tvKubiNum.setText(user.getKubiNum() + "游币");
        mViewPager = (ViewPager) view.findViewById(Rx.id.viewpager);
        mTabPageIndicator = (TabPageIndicator) view.findViewById(Rx.id.tabindicator);
        mUnderlinePageIndicatorEx = (UnderlinePageIndicatorEx) view
                .findViewById(Rx.id.underlineindicator);
        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(CouponListFragment.newInstance(CouponListFragment.TYPE_USABLE));
        fragments.add(CouponListFragment.newInstance(CouponListFragment.TYPE_HISTORY));

        CommonViewPagerAdapter adapter = new CommonViewPagerAdapter(getChildFragmentManager());
        adapter.setFragments(fragments, new String[] {
                "可用代金券", "历史代金券"
        });

        mViewPager.setAdapter(adapter);

        mTabPageIndicator.setViewPager(mViewPager);
        mTabPageIndicator.setOnPageChangeListener(mUnderlinePageIndicatorEx);

        if (SDKControler.getActivityOrientation() == ActivityOrientation.LANDSCAPE) {
            AppUtil.dip2px(context, 30);
            rlTopLayout.setPadding(AppUtil.dip2px(context, 30), AppUtil.dip2px(context, 10), 0,
                    AppUtil.dip2px(context, 10));
            mUnderlinePageIndicatorEx.setWidth(4);
        } else {
            rlTopLayout.setPadding(AppUtil.dip2px(context, 15), AppUtil.dip2px(context, 10), 0,
                    AppUtil.dip2px(context, 10));
            mUnderlinePageIndicatorEx.setWidth(-1);
        }
        mUnderlinePageIndicatorEx.setViewPager(mViewPager);
        mUnderlinePageIndicatorEx.setFades(false);

    }

    private void updateView() {
        tvKubiNum.setText(user.getKubiNum() + "游币");
    }

    @Override
    public void onResume() {
        super.onResume();
        updateView();
    }
}
